package org.fanximall.erp.product.application.service.impl;

import org.apache.commons.collections4.CollectionUtils;
import org.fanximall.erp.common.tree.TreeDefinitionModel;
import org.fanximall.erp.common.utils.ExceptionUtils;
import org.fanximall.erp.product.application.convert.CategoryConvert;
import org.fanximall.erp.product.application.req.CategorySaveCommand;
import org.fanximall.erp.product.application.resp.CategoryTreeRespDTO;
import org.fanximall.erp.product.application.service.CategoryService;
import org.fanximall.erp.product.domain.model.ProductCategoryModel;
import org.fanximall.erp.product.domain.model.ProductLeafCategoryModel;
import org.fanximall.erp.product.domain.repository.CategoryRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author :  william
 * @version :  V1.0  created in 2024/5/6
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryRepository categoryRepository;

    @Resource
    private CategoryConvert categoryConvert;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createCategory(CategorySaveCommand categorySaveCommand) {
        categorySaveCommand.validate();
        List<ProductCategoryModel> categoryByCategoryName = categoryRepository.getCategoryByCategoryName(categorySaveCommand.getNameCn(), categorySaveCommand.getNameEn());
        ExceptionUtils.ifTrueThrowVerifyException(CollectionUtils.isNotEmpty(categoryByCategoryName), "类目名称不能重复");
        ProductCategoryModel productCategoryModel = categoryConvert.productCategoryModelConvert(categorySaveCommand);
        categoryRepository.saveCategory(productCategoryModel);
        if (categorySaveCommand.getLeaf()) {
            ProductLeafCategoryModel productLeafCategoryModel = categoryConvert.productLeafCategoryModelConvert(categorySaveCommand.getLeafCategorySaveCommand());
            productLeafCategoryModel.setCategoryId(productCategoryModel.getCategoryId());
            categoryRepository.saveLeafCategory(productLeafCategoryModel);
        }
    }

    @Override
    public void updateCategoryLevel(Long categoryId, Long parentCategoryId) {
        ProductCategoryModel productCategoryModel = new ProductCategoryModel();
        productCategoryModel.setCategoryId(categoryId);
        productCategoryModel.setParentId(parentCategoryId);
        categoryRepository.updateCategory(productCategoryModel);
    }

    @Override
    public void updateCategoryState(Long categoryId, Integer state) {
        ProductCategoryModel productCategoryModel = new ProductCategoryModel();
        productCategoryModel.setCategoryId(categoryId);
        productCategoryModel.setState(state);
        categoryRepository.updateCategory(productCategoryModel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCategory(CategorySaveCommand categorySaveCommand) {
        categoryRepository.updateCategory(categoryConvert.productCategoryModelConvert(categorySaveCommand));
        if (null != categorySaveCommand.getLeafCategorySaveCommand()) {
            categoryRepository.updateLeafCategory(categoryConvert.productLeafCategoryModelConvert(categorySaveCommand.getLeafCategorySaveCommand()));
        }
    }

    @Override
    public List<TreeDefinitionModel<CategoryTreeRespDTO>> getTreeByStateAndName(Integer state, String name) {
        List<ProductCategoryModel> productCategoryModels = categoryRepository.getAllCategory(state, name);
        return TreeDefinitionModel.buildTreeDefinitionModel(categoryConvert.treeDefinitionModelBuildListConvert(productCategoryModels));
    }

    @Override
    public TreeDefinitionModel<CategoryTreeRespDTO> getParentNode(Long categoryId) {
        List<ProductCategoryModel> parentNode = categoryRepository.getAllParent(categoryId);
        return TreeDefinitionModel.buildSingleTreeDefinitionModel(categoryConvert.treeDefinitionModelBuildListConvert(parentNode));
    }
}
